﻿using Coleotrack.Service.Contracts;
using NHibernate;

namespace Coleotrack.Service
{
    public class NHibernateUnitOfWork : INHibernateUnitOfWork
    {
        private readonly ITransaction transaction;

        public NHibernateUnitOfWork(ISession session)
        {
            CurrentSession = session;
            CurrentSession.FlushMode = FlushMode.Commit;
            transaction = CurrentSession.BeginTransaction();
        }

        public void Dispose()
        {
            //CurrentSession.Close();
        }

        public void Commit()
        {
            if (transaction != null && transaction.IsActive)
            {
                transaction.Commit();
            }
        }

        public void Rollback()
        {
            if (transaction != null && transaction.IsActive)
            {
                transaction.Rollback();
            }
        }

        public ISession CurrentSession { get; private set; }
    }
}